package com.woniuxy.controller;


import com.woniuxy.ResponseResult;
import com.woniuxy.ServerResponse;
import com.woniuxy.entity.Shopinfo;
import com.woniuxy.service.ShopinfoService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2021-03-23
 */
@Controller
@CrossOrigin
@RequestMapping("/shopinfo")
@Validated
@Slf4j
@SuppressWarnings("all")
public class  ShopinfoController {

   @Autowired
   private RabbitTemplate rabbitTemplate;
    @Autowired
    private ShopinfoService shopinfoService;

    /**
     * 注册
     * @param shopinfo
     * @param bindingResult
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public ServerResponse<Void> register(@Valid @RequestBody  Shopinfo shopinfo,BindingResult bindingResult){
        rabbitTemplate.convertAndSend("mailExchange","mailKey",shopinfo.getAccount());
        // 进行md5加密
        shopinfo.setPassword(DigestUtils.md5Hex(shopinfo.getPassword())).setRegisterTime(new Date())
        .setStatus("1");
         log.info("dodo");

       ServerResponse<Void> response = shopinfoService.register(shopinfo);
        return response;
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/updateAvator")
    @ResponseBody
    public ResponseResult<Void> updateAvator (@NotNull(message = "文件不能为空")MultipartFile file) throws Exception{


        ResponseResult<Void> result = shopinfoService.updateAvator(file);
        return result;
    }

    /**
     * 账号密码登录
     * @param account
     * @param password
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ServerResponse<Void> Login(@NotNull(message = "账号不能为空") String account,@NotNull(message = "密码不能为空") String password){
        UsernamePasswordToken token = new UsernamePasswordToken(account,DigestUtils.md5Hex(password));
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
//            e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(404,"账号不存在");
        }catch (IncorrectCredentialsException e){
            return ServerResponse.createByErrorCodeMessage(504,"密码不正确");
        }
        return ServerResponse.createBySuccessMessage("登陆成功");
    }

    /**
     * 验证码登录
     * @param account
     * @return
     */
    @RequestMapping("/codeLogin")
    @ResponseBody
    public ServerResponse<Void> codeLogin(String account,String code){


        return shopinfoService.codeLogin(account,code);
    }
    @RequestMapping("/sendCode")
    @ResponseBody
    public ServerResponse<Void> send(@NotNull(message = "账号不能为空") String account){

        return shopinfoService.send(account);
    }

}

